/*
 @description: 用户列表增删查改
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")
class UserController extends Controller {
    async index() { //搜索
        const {
            ctx,
            app
        } = this;
        const {
            keyname,
            page = 1,
            number = 4
        } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id,name,password,nickname,sex,phone,head,history from user where name like '%${keyname}%' limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from user where name like '%${keyname}%'`);
        } else if (keyname) {
            data = await app.mysql.query(`select id,name,password,nickname,sex,phone,head,history from user where name ='${keyname}' or name  like '%${keyname}%' limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from user where name like '%${keyname}%' or name like '%${keyname}%'`);
        } else {
            data = await app.mysql.query(`select id,name,password,nickname,sex,phone,head,history from user limit ${offset},${number}`);
            count = await app.mysql.query(`select count(*) as count from user`);
        }
        if (count[0].count != 0) {
            ctx.body = {
                code: 0,
                msg: "查找成功",
                data,
                count: count[0].count
            }
        } else {
            ctx.body = {
                code: 4004,
                msg: "查找失败"
            }
        }
    }
    //通过id 来获取选到的数据
    async show() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.get('user', {
            ID: id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: '查找成功',
                data: result
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "查找失败"
            }
        }
    }
    //添加
    async create() { //post
        const {
            ctx,
            app
        } = this;
        const {
            name,
            password,
            sex,
            phone,
            head,
                 
        } = ctx.request.body
        if (name && password && sex && phone && head && history) {
            const confPwd = utility.md5(password)
            const data = await app.mysql.insert('user', {
                name,
                password:confPwd,
                sex,
                phone,
                head,
                history,
                nickname: Date.now()
            });
            ctx.body = {
                code: 0,
                msg: "新增成功",
            };
        } else {
            ctx.body = {
                code: 4006,
                msg: "缺少必要参数，添加失败",
            };
        }
    }
    //改
    async update() { //put
        const {
            ctx,
            app
        } = this;
        const {
            name,
            password,
            sex,
            phone,
            head,
            history
        } = ctx.request.body
        const {
            id
        } = ctx.params;
        const row = {
            name: name,
            password: password,
            sex,
            phone: sex,
            phone,
            head: head,
            history: history,
            nickname: Date.now()
        };
        const options = { //通过这个id 来选定的修改数据
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('user', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: "编辑成功",
            };
        } else {
            ctx.body = {
                code: 4008,
                msg: "编辑失败",
            };
        }
    }
    //删除
    async destroy() { //DELETE
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.delete(`user`, {
            id: +id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            };
        }
    }

}
module.exports = UserController